/*
 * Copyright (c) 2016. zhaojin 15398699939@163.com
 */

package com.bankscene.screenfit.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.transition.Slide;
import android.view.Gravity;
import android.widget.Toast;

import com.bankscene.screenfit.R;
import com.bankscene.screenfit.core.ActivityStackManager;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * This provides methods to help Activities load their UI.
 */
public class ActivityUtils {

    /**
     * The {@code fragment} is added to the container rootView with id {@code frameId}. The operation is
     * performed by the {@code fragmentManager}.
     */
    public static void addFragmentToActivity(@NonNull FragmentManager fragmentManager,
                                             @NonNull Fragment fragment, int frameId) {
        checkNotNull(fragmentManager);
        checkNotNull(fragment);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(frameId, fragment);
        transaction.commit();
    }

    public static void replaceFragmentToActivity(@NonNull FragmentManager fragmentManager,
                                                 @NonNull Fragment fragment, int frameId) {
        checkNotNull(fragmentManager);
        checkNotNull(fragment);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(frameId, fragment);
        transaction.commit();
    }

    public static void replaceFragmentToActivityAndAddToBackStack(@NonNull FragmentManager fragmentManager,
                                                                  @NonNull Fragment fragment, int frameId) {
        checkNotNull(fragmentManager);
        checkNotNull(fragment);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(frameId, fragment, fragment.getClass().getName());//添加一个tag方便之后查找
        transaction.addToBackStack(fragment.getClass().getName());
        transaction.commit();
    }

    public static void logout(ActivityStackManager asm) {
        Activity activity = asm.peek();
        Toast.makeText(activity, activity.getResources().getText(R.string.thanks_use), Toast.LENGTH_SHORT).show();
        asm.appExit();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static void setupWindowAnimations(Activity activity) {
        try {
            // Re-enter transition is executed when returning to this activity
            Slide slideTransition = new Slide();
            slideTransition.setSlideEdge(Gravity.LEFT);
            slideTransition.setDuration(activity.getResources().getInteger(R.integer.anim_duration_long));
            activity.getWindow().setReenterTransition(slideTransition);
            activity.getWindow().setExitTransition(slideTransition);
        }catch (NoClassDefFoundError error){
            error.printStackTrace();
        }
    }
}
